﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Point = System.Windows.Point;


namespace weighing
{
    /// <summary>
    /// DragUc.xaml 可以拖动的控件
    /// </summary>
    public partial class DragUc : System.Windows.Controls.UserControl
    {
        private Canvas canvas;
        private bool isDragging = false;
        private Point mouseOffset;
        public bool isCanDrag = true;


        #region Property
        public string LabelTitle { get; set; }
 
        public Visibility TitleVisibility { get; set; }
        public Visibility VallueVisibility { get; set; }
        public Visibility ColonsVisibility { get; set; }

        public double TitleFontSize { get; set; }
        public double ValueFontSize { get; set; }

        private WrapPanel wrapPanel;
      
        public WrapPanel MainWrapPanel
        {
            get
            {
                if (wrapPanel == null)
                {
                    Border border = this.Content as Border;
                    wrapPanel = border.Child as WrapPanel;
                }
                return wrapPanel;
            }
            set { wrapPanel = value; }
        }
        #endregion



        public String TextValue
        {
            get { return (String)GetValue(TextValueProperty); }
            set { SetValue(TextValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextValueProperty =
            DependencyProperty.Register("TextValue", typeof(String), typeof(DragUc), new PropertyMetadata("", OnTextValueChange));

        private static void OnTextValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        public DragUc()
        {
            InitializeComponent();
            this.Loaded += DragUc_Loaded;
            this.Unloaded += DragUc_Unloaded;
            this.DataContext = this;
            this.Clip = null;
        }


        #region drag event
        private void DragUc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (canvas == null)
            {
                canvas = VisualTreeHelper.GetParent(this) as Canvas;
            }
            if (canvas != null)
            {
                isDragging = true;
                mouseOffset = e.GetPosition(this);
                this.CaptureMouse();
            }
        }

        private void DragUc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                this.ReleaseMouseCapture();
                isDragging = false;
            }
        }
        private void DragUc_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if(isCanDrag == false) return;
            if (canvas != null && isDragging == true)
            {
                Point p = e.GetPosition(canvas);
                this.SetValue(Canvas.TopProperty, p.Y - mouseOffset.Y);
                this.SetValue(Canvas.LeftProperty, p.X - mouseOffset.X);
            }
        }
        private void DragUc_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            this.CaptureMouse();
            switch (e.Key)
            {
                case Key.Left:
                    this.SetValue(Canvas.LeftProperty, Canvas.GetLeft(this) - 1);
                    break;
                case Key.Up:
                    this.SetValue(Canvas.TopProperty, Canvas.GetTop(this) - 1);
                    break;
                case Key.Right:
                    this.SetValue(Canvas.LeftProperty, Canvas.GetLeft(this) + 1);
                    break;
                case Key.Down:
                    this.SetValue(Canvas.TopProperty, Canvas.GetTop(this) + 1);
                    break;
            }
            this.ReleaseMouseCapture();
        }
        #endregion

        #region user content event

        private void DragUc_Loaded(object sender, RoutedEventArgs e)
        {
            this.MouseLeftButtonDown += DragUc_MouseLeftButtonDown; ;
            this.MouseLeftButtonUp += DragUc_MouseLeftButtonUp; ;
            this.MouseMove += DragUc_MouseMove; ;
            this.KeyUp += DragUc_KeyUp;
        }

        private void DragUc_Unloaded(object sender, RoutedEventArgs e)
        {
            this.MouseLeftButtonDown -= DragUc_MouseLeftButtonDown;
            this.MouseLeftButtonUp -= DragUc_MouseLeftButtonUp;
            this.MouseMove -= DragUc_MouseMove;
            this.KeyUp -= DragUc_KeyUp;
        }

        public void SetTitleText(String title)
        {
            TextBlock tb = MainWrapPanel.Children[0] as TextBlock;
            tb.Text = title;
        }
        public string GetTitleText()
        {
            TextBlock tb = MainWrapPanel.Children[0] as TextBlock;
            return tb.Text;
        }
        public void SetValueText(String val)
        {
            TextBlock tb = MainWrapPanel.Children[2] as TextBlock;
            tb.Text = val;
        }
        public string GetValueText()
        {
            TextBlock tb = MainWrapPanel.Children[2] as TextBlock;
            return tb.Text;
        }
        public double GetTitleFontSize()
        {
            TextBlock tb = MainWrapPanel.Children[0] as TextBlock;
            return tb.FontSize;
        }

        public double GetValueFontSize()
        {
            TextBlock tb = MainWrapPanel.Children[2] as TextBlock;
            return tb.FontSize;
        }

        public void SetTitleFontSize(double size)
        {
            TextBlock tb = MainWrapPanel.Children[0] as TextBlock;
            tb.FontSize = size;
        }

        public void SetValueFontSize(double size)
        {
            TextBlock tb = MainWrapPanel.Children[2] as TextBlock;
            tb.FontSize = size;
        }

        public void SetTitleVisable(bool show)
        {
            TextBlock tb = MainWrapPanel.Children[0] as TextBlock;
            TextBlock colonsTb = MainWrapPanel.Children[1] as TextBlock;
            if (show)
            {
                tb.Visibility = Visibility.Visible;
                colonsTb.Visibility = Visibility.Visible;
            }
            else
            {
                tb.Visibility = Visibility.Collapsed;
                colonsTb.Visibility = Visibility.Collapsed;
            }
        }

        public void SetValueVisable(bool show)
        {
            TextBlock tb = MainWrapPanel.Children[2] as TextBlock;
            if (show)
            {
                tb.Visibility = Visibility.Visible;
            }
            else
            {
                tb.Visibility = Visibility.Collapsed;
            }
        }
        #endregion
    }
}
